//Replication code for "Unpacking Legitimacy Perceptions of Investor-State Dispute Settlement: Effects of Outcome and Procedure" ///
***Analysis run using Stata 15***

use "isds_data.dta", clear

*install packages
ssc install cibar
ssc install outreg2
ssc install blindschemes

set scheme plotplain

* New Variables for Analyses 

** Age Group Variables
gen age_groups_ger = .
replace age_groups_ger = 1 if age >= 18 & age <= 29 & country == 1
replace age_groups_ger = 2 if age >= 30 & age <= 39 & country == 1
replace age_groups_ger = 3 if age >= 40 & age <= 49 & country == 1
replace age_groups_ger = 4 if age >= 50 & age <= 59 & country == 1
replace age_groups_ger = 5 if age >= 60 & age <= 69 & country == 1

label variable age_groups_ger "Age Groups Germany" 
label define agegroups 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69"
label values age_groups_ger agegroups

gen age_groups_fr = .
replace age_groups_fr = 1 if age >= 18 & age <= 29 & country == 2
replace age_groups_fr = 2 if age >= 30 & age <= 39 & country == 2
replace age_groups_fr = 3 if age >= 40 & age <= 49 & country == 2
replace age_groups_fr = 4 if age >= 50 & age <= 59 & country == 2
replace age_groups_fr = 5 if age >= 60 & age <= 69 & country == 2

label variable age_groups_fr "Age Groups France" 
label values age_groups_fr agegroups

gen age_groups_us = .
replace age_groups_us = 1 if age >= 18 & age <= 29 & country == 3
replace age_groups_us = 2 if age >= 30 & age <= 39 & country == 3
replace age_groups_us = 3 if age >= 40 & age <= 49 & country == 3
replace age_groups_us = 4 if age >= 50 & age <= 59 & country == 3
replace age_groups_us = 5 if age >= 60 & age <= 74 & country == 3

label variable age_groups_us "Age Groups USA"
label define agegroups_74 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-74" 
label values age_groups_us agegroups_74

** Education Variables
mvdecode educ3ger, mv(13)
mvdecode educfr, mv(10)
mvdecode educusa, mv(15)

generate college_ger = educ3ger >= 9 if !mi(educ3ger) 
generate college_fr = educfr >= 8 if !mi(educfr) 
generate college_usa = educusa >= 12 if !mi(educusa)

generate edu_cat = college_ger == 1 | college_fr == 1 | college_usa == 1 
label variable edu_cat "Education"
label define degree 0 "no tertiary education" 1 "bachelor degree or more" 
label values edu_cat degree

gen edu_cat_age_25_64 = . 
replace edu_cat_age_25_64 = edu_cat if age >= 25 & age <= 64
label variable edu_cat_age_25_64 "Educuation 25-64"
label values edu_cat_age_25_64 degree 

** Income Groups Variable
xtile q4incger = mincger, nq(3) 
xtile q4incfr = mincfr, nq(3) 
xtile q4incusa = mincusa, nq(3) 

generate income_cat = 1 if q4incger==1 | q4incfr==1 | q4incusa==1
replace income_cat = 2 if q4incger==2 | q4incfr==2 | q4incusa==2
replace income_cat = 3 if q4incger==3 | q4incfr==3 | q4incusa==3

** Grouping Variable for Globalization 
mvdecode globalization, mv(6)
generate glob_cat = (globalization > 3) if !mi(globalization)
label variable glob_cat "Attitudes Globalization"

* Main Analyses

table treatment t_dispute // to create Table 1 showing participants in each treatment group

** Figure 1 
cibar disleg, over(t_dispute treatment) bargap(20) ///
graphopts(ytitle("Legitimacy beliefs") ///
legend(size(small) rows(2)) ///
ylabel(2.6(.2)4.2) graphregion(color(white)))
// manually rename labels "Home firm wins against foreign government", "Foreign firm wins against home government"

** Figure 2
regress disleg i.t_dispute##i.treatment
coefplot, drop(_cons) xline(0) ///
xlabel(-0.8(.2)0.8) ///
graphregion(color(white)) xtitle("ATE")

** Figure 3
eststo: regress disleg i.t_dispute##i.treatment if country==2, base
coefplot, drop(_cons) xline(0) ///
title("France") graphregion(color(white)) ///
xlabel(-0.8(.2)0.8) ///
name(graph1, replace) 

eststo: regress disleg i.t_dispute##treatment if country==1
coefplot, drop(_cons) xline(0) /// 
title("Germany") graphregion(color(white)) /// 
xlabel(-0.8(.2)0.8) ///
name(graph2, replace)

eststo: regress disleg i.t_dispute##treatment if country==3
coefplot, drop(_cons) xline(0) ///
title("USA") graphregion(color(white)) ///
xlabel(-0.8(.2)0.8) ///
name(graph3, replace)

graph combine graph1 graph2 graph3, xcommon rows(3) graphregion(color(white))

eststo clear


* Additional Analyses (Supplementary Material)

** Sample Characteristics (Supplementary Material B)

*** Table B.1 
tab1 age_groups_fr gender edu_cat_age_25_64 if country==2

*** Table B.2
tab1 age_groups_ger gender edu_cat_age_25_64 if country==1

*** Table B.3 
tab1 age_groups_us gender edu_cat_age_25_64 if country==3


** Descriptive Statitics (Table C.1)

table treatment country, contents(mean disleg sd disleg freq) by(t_dispute) col

** Pairwise comparison tests (Supplementary Material D)

anova disleg i.t_dispute i.treatment i.t_dispute##i.treatment
pwcompare t_dispute##treatment, effects
contrast r.t_dispute@treatment // t-tests

** Regressions

*** Table E.1
regress disleg i.t_dispute##i.treatment

*** Table E.2
regress disleg i.t_dispute##i.treatment if country==2

*** Table E.3
regress disleg i.t_dispute##i.treatment if country==1

*** Table E.4
regress disleg i.t_dispute##i.treatment if country==3


** Subgroup analyses (Supplementary Material F)

*** by education (Figure F.4)
regress disleg i.t_dispute##i.treatment if edu_cat == 1
estimates store bachelor_est

regress disleg i.t_dispute##i.treatment if edu_cat == 0
estimates store nobachelor_est

coefplot (bachelor_est) (nobachelor_est), drop (_cons) xline(0) xlabel(-0.8(.2)0.8)
//manually rename labels "tertiary education", "no tertiary education"

*** by income (Figure F.5)
regress disleg i.t_dispute##i.treatment if income_cat == 1
estimates store low

regress disleg i.t_dispute##i.treatment if income_cat == 2
estimates store middle

regress disleg i.t_dispute##i.treatment if income_cat == 3
estimates store high

coefplot (low) (middle) (high), drop (_cons) xline(0) xlabel(-1(.2)1)

*** by globalization (Figure F.6)
regress disleg i.t_dispute##i.treatment if glob_cat == 1
estimates store negative

regress disleg i.t_dispute##i.treatment if glob_cat == 0
estimates store positive

coefplot (negative) (positive), drop (_cons) xline(0) xlabel(-1(.2)1)
